const { VUE_APP_API } = process.env
export default {
  // 获取文件图标
  getImg (data, name) {
    name = name || data.fileName || data.filePath
    let extensionArr = name.split('.')
    let extension = extensionArr[extensionArr.length - 1]
    let url = ''
    switch (extension) {
      case 'xls':
      case 'xlsx':
        url = 'img/upload/excel.png'
        break
      case 'doc':
      case 'docx':
        url = 'img/upload/word.png'
        break
      case 'zip':
      case 'rar':
        url = 'img/upload/zip.png'
        break
      case 'pdf':
        url = 'img/upload/pdf.png'
        break
      case 'ppt':
      case 'pptx':
        url = 'img/upload/ppt.png'
        break
      case 'mp4':
        url = 'img/upload/mp4.png'
        break
      default:
        if (data.fileName) {
          delete data.fileName
        }
        url = process.env.VUE_APP_API + '/file/download?jsonStr= ' + encodeURI(JSON.stringify(data))
        break
    }
    return url
  },
  // 根据文件名判断是否为图片
  isImg (name) {
    let extensionArr = name.split('.')
    let extension = extensionArr[extensionArr.length - 1]
    let flag = false
    switch (extension.toLowerCase()) {
      case 'png':
      case 'jpg':
      case 'jpeg':
      case 'gif':
      case 'bmp':
        flag = true
        break
      default:
        // statements_def
        break
    }
    return flag
  },
  // 图片加错误
  defaultImg () {
    // return 'this.src="' + require('@/assets/common/bannber-bg.png') + '"'
  },
  // 用户默认头像
  defaultUserPhoto () {
    // return 'this.src="' + require('@/assets/default-logo.png') + '"'
  },
  // 获取游览器缓存信息的大小
  getCacheSize (lx) {
    // lx是localStorage还是sessionStorage
    lx = lx === undefined ? 'l' : lx
    let obj = ''
    if (lx === 'l') {
      if (!window.localStorage) {
        console.log('游览器不支持localStorage')
      } else {
        obj = window.localStorage
      }
    } else {
      if (!window.sessionStorage) {
        console.log('游览器不支持sessionStorage')
      } else {
        obj = window.sessionStorage
      }
    }
    if (obj !== '') {
      let size = 0
      for (let item in obj) {
        if (obj.hasOwnProperty(item)) {
          size += obj.getItem(item).length
        }
      }
      return (size / 1024).toFixed(2) + 'KB'
    }
    return false
  },
  // 导出  下载
  exportFile (url, data, _method = 'post') {
    if (!url) { console.error('下载地址错误！'); return true }
    url = `${VUE_APP_API}${url}`
    let form = document.createElement('form')
    form.setAttribute('action', url)
    form.setAttribute('method', _method)
    for (let item in data) {
      let val = data[item] === undefined ? '' : data[item] === null ? '' : data[item]
      let input = document.createElement('input')
      input.setAttribute('type', 'text')
      input.setAttribute('name', item)
      input.setAttribute('value', val)
      form.appendChild(input)
    }
    document.body.appendChild(form)
    form.submit()
    document.body.removeChild(form)
  }
}
